<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Api\Traits\FormatBaseDate;
use Illuminate\Support\Facades\DB;

class AdminRole extends Model
{
    use HasFactory, FormatBaseDate;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'slug',
        'sort',
        'memo',
    ];

    /**
     * 拥有此角色的用户 [定义多对多的反向关联]
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users()
    {
        return $this->belongsToMany(AdminUser::class, 'admin_role_user', 'user_id', 'role_id');
    }

    /**
     * 获取角色列表
     * @return array
     */
    public static function fetchRoles()
    {
        return DB::table('admin_roles')
            ->orderBy('sort', 'asc')
            ->get()
            ->toArray();
    }
}
